#!/usr/bin/python3
import os
import subprocess

def find_and_execute_make(start_dir):
    for root, dirs, files in os.walk(start_dir):
        if 'Makefile' in files:
            print(f"Found Makefile in {root}. Executing make...")
            try:
                # 切换到该目录并执行make命令
                os.chdir(root)
                subprocess.run(['make'], check=True)
                print(f"make completed successfully in {root}")
            except subprocess.CalledProcessError as e:
                print(f"make failed in {root} with error: {e}")
            except Exception as e:
                print(f"An error occurred in {root}: {e}")
            finally:
                # 恢复原始工作目录（可选）
                os.chdir(start_dir)

if __name__ == "__main__":
    # 从当前目录开始遍历
    current_dir = os.getcwd()
    find_and_execute_make(current_dir)
